import os
from PIL import Image
import sys

def convert_webp_to_jpg(input_path, output_main, output_thumb):
    # 打开webp图片
    with Image.open(input_path) as img:
        # 保存主图片
        img.convert("RGB").save(output_main, "JPEG")
        
        # 创建缩略图并保存
        img.thumbnail((240, 180))
        img.convert("RGB").save(output_thumb, "JPEG")

def main():
    # 获取当前脚本所在目录
    current_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
    
    # 查找当前目录中的webp文件
    webp_files = [f for f in os.listdir(current_dir) if f.lower().endswith('.webp')]
    
    if not webp_files:
        print("未找到webp文件。")
        return
    
    for webp_file in webp_files:
        input_path = os.path.join(current_dir, webp_file)
        output_main = os.path.join(current_dir, "cover-main.jpg")
        output_thumb = os.path.join(current_dir, "cover-thumb.jpg")
        
        try:
            convert_webp_to_jpg(input_path, output_main, output_thumb)
            print(f"已成功转换 {webp_file}")
        except Exception as e:
            print(f"转换 {webp_file} 时出错: {str(e)}")

if __name__ == "__main__":
    main()